<?php
// src/Asiah/SiklusBundle/Entity/User.php
namespace Asiah\SiklusBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
 * @ORM\Entity(repositoryClass="Asiah\SiklusBundle\Repository\UserRepository")
 * @ORM\Table(name="user")
 */

class User implements UserInterface{
	public function __construct() {
		$this -> bookmarks = new ArrayCollection();
		$this -> subscriber = new ArrayCollection();
		$this -> subscribing = new ArrayCollection();
		$this -> setJoinDate(new \DateTime());
		$this -> userRoles = new ArrayCollection();
		$this->setSalt(md5(time()));
	}

	/**
	 * @ORM\Id
	 * @ORM\Column(type="integer")
	 * @ORM\GeneratedValue(strategy="AUTO")
	 */
	protected $id;
	/**
	 * @ORM\Column(type="string", length="50")
	 */
	protected $name;
	/**
	 * @ORM\Column(type="string", unique=true, length="20")
	 */
	protected $username;
	/**
	 * @ORM\Column(type="string")
	 */
	protected $password;
	/**
	 * @ORM\Column(type="string")
	 */
	protected $email;
	/**
	 * @ORM\Column(type="string", length="255")
	 *
	 * @var string salt
	 */
	protected $salt;
	/**
	 * @ORM\Column(type="string",length=255, nullable=true)
	 */
	protected $photo;
	/**
	 * @ORM\Column(type="string",length=255, nullable=true)
	 */
	protected $photopath;
	/**
	 * @Assert\File(maxSize="6000000")
	 */
	public $file;
	/**
	 * @ORM\Column(type="date")
	 */
	protected $join_date;

	/**
	 * @ORM\OneToMany(targetEntity="Bookmark", mappedBy="user")
	 */
	protected $bookmarks;

	/**
	 * @ORM\ManyToMany(targetEntity="Comment", mappedBy="users")
	 */
	protected $comments;

	/**
	 * @ORM\ManyToMany(targetEntity="User", mappedBy="subscribing")
	 */
	private $subscriber;

	/**
	 * @ORM\ManyToMany(targetEntity="User", inversedBy="subscriber")
	 * @ORM\JoinTable(name="subscriber",
	 * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
	 * inverseJoinColumns={@ORM\JoinColumn(name="follower_user_id", referencedColumnName="id" )})
	 */
	private $subscribing;
	/**
	 * @ORM\ManyToMany(targetEntity="Role")
	 * @ORM\JoinTable(name="user_role",
	 *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
	 *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
	 * )
	 *
	 * @var ArrayCollection $userRoles
	 */
	protected $userRoles;
	/**
	 * Get id
	 *
	 * @return integer
	 */
	public function getId() {
		return $this -> id;
	}

	/**
	 * Set name
	 *
	 * @param string $name
	 */
	public function setName($name) {
		$this -> name = $name;
	}

	/**
	 * Get name
	 *
	 * @return string
	 */
	public function getName() {
		return $this -> name;
	}

	/**
	 * Set username
	 *
	 * @param string $username
	 */
	public function setUsername($username) {
		$this -> username = $username;
	}

	/**
	 * Get username
	 *
	 * @return string
	 */
	public function getUsername() {
		return $this -> username;
	}

	/**
	 * Set password
	 *
	 * @param string $password
	 */
	public function setPassword($password) {
		$this -> password = $password;
	}

	/**
	 * Get password
	 *
	 * @return string
	 */
	public function getPassword() {
		return $this -> password;
	}

	/**
	 * Set email
	 *
	 * @param string $email
	 */
	public function setEmail($email) {
		$this -> email = $email;
	}

	/**
	 * Get email
	 *
	 * @return string
	 */
	public function getEmail() {
		return $this -> email;
	}

	/**
	 * Set photo
	 *
	 * @param string $photo
	 */
	public function setPhoto($photo) {
		$this -> photo = $photo;
	}

	/**
	 * Get photo
	 *
	 * @return string
	 */
	public function getPhoto() {
		return $this -> photo;
	}

	/**
	 * Set join_date
	 *
	 * @param date $joinDate
	 */
	public function setJoinDate($joinDate) {
		$this -> join_date = $joinDate;
	}

	/**
	 * Get join_date
	 *
	 * @return date
	 */
	public function getJoinDate() {
		return $this -> join_date;
	}

	/**
	 * Add bookmarks
	 *
	 * @param Asiah\SiklusBundle\Entity\Bookmark $bookmarks
	 */
	public function addBookmarks(\Asiah\SiklusBundle\Entity\Bookmark $bookmarks) {
		$this -> bookmarks[] = $bookmarks;
	}

	/**
	 * Get bookmarks
	 *
	 * @return Doctrine\Common\Collections\Collection
	 */
	public function getBookmarks() {
		return $this -> bookmarks;
	}

	/**
	 * Add comments
	 *
	 * @param Asiah\SiklusBundle\Entity\Comment $comments
	 */
	public function addComments(\Asiah\SiklusBundle\Entity\Comment $comments) {
		$this -> comments[] = $comments;
	}

	/**
	 * Get comments
	 *
	 * @return Doctrine\Common\Collections\Collection
	 */
	public function getComments() {
		return $this -> comments;
	}

	/**
	 * Add subscriber
	 *
	 * @param Asiah\SiklusBundle\Entity\User $subscriber
	 */
	public function addSubscriber(\Asiah\SiklusBundle\Entity\User $subscriber) {
		$this -> subscriber[] = $subscriber;
	}

	/**
	 * Get subscriber
	 *
	 * @return Doctrine\Common\Collections\Collection
	 */
	public function getSubscriber() {
		return $this -> subscriber;
	}

	/**
	 * Add subscribing
	 *
	 * @param Asiah\SiklusBundle\Entity\User $subscribing
	 */
	public function addSubscribing(\Asiah\SiklusBundle\Entity\User $subscribing) {
		$this -> subscribing[] = $subscribing;
	}

	/**
	 * Get subscribing
	 *
	 * @return Doctrine\Common\Collections\Collection
	 */
	public function getSubscribing() {
		return $this -> subscribing;
	}

	/**
	 * Set salt
	 *
	 * @param string $salt
	 */
	public function setSalt($salt) {
		$this -> salt = $salt;
	}

	/**
	 * Get salt
	 *
	 * @return string
	 */
	public function getSalt() {
		return $this -> salt;
	}

	/**
	 * Add userRoles
	 *
	 * @param Asiah\SiklusBundle\Entity\Role $userRoles
	 */
	public function addUserRoles(\Asiah\SiklusBundle\Entity\Role $userRoles) {
		$this -> userRoles[] = $userRoles;
	}

	/**
	 * Get userRoles
	 *
	 * @return Doctrine\Common\Collections\Collection
	 */
	public function getUserRoles() {
		return $this -> userRoles;
	}

	/**
	 * Erases the user credentials.
	 */
	public function eraseCredentials() {

	}

	/**
	 * Gets an array of roles.
	 *
	 * @return array An array of Role objects
	 */
	public function getRoles() {
		return $this -> getUserRoles() -> toArray();
	}

	/**
	 * Compares this user to another to determine if they are the same.
	 *
	 * @param UserInterface $user The user
	 * @return boolean True if equal, false othwerwise.
	 */
	public function equals(UserInterface $user) {
		return md5($this -> getUsername()) == md5($user -> getUsername());
	}

	public function getAbsolutePath()
	{
		return null === $this->photopath ? null : $this->getUploadRootDir().'/'.$this->photopath;
	}

	public function getWebPath()
	{
		return null === $this->photopath ? null : $this->getUploadDir().'/'.$this->photopath;
	}

	protected function getUploadRootDir()
	{
		// the absolute directory path where uploaded documents should be saved
		return __DIR__.'/../../../../web/'.$this->getUploadDir();
	}

	protected function getUploadDir()
	{
		// get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
		return 'uploads/documents';
	}

    /**
     * Set photopath
     *
     * @param string $photopath
     */
    public function setPhotopath($photopath)
    {
        $this->photopath = $photopath;
    }

    /**
     * Get photopath
     *
     * @return string 
     */
    public function getPhotopath()
    {
        return $this->photopath;
    }

    /**
     * Add bookmarks
     *
     * @param Asiah\SiklusBundle\Entity\Bookmark $bookmarks
     */
    public function addBookmark(\Asiah\SiklusBundle\Entity\Bookmark $bookmarks)
    {
        $this->bookmarks[] = $bookmarks;
    }

    /**
     * Add comments
     *
     * @param Asiah\SiklusBundle\Entity\Comment $comments
     */
    public function addComment(\Asiah\SiklusBundle\Entity\Comment $comments)
    {
        $this->comments[] = $comments;
    }

    /**
     * Add subscriber
     *
     * @param Asiah\SiklusBundle\Entity\User $subscriber
     */
    public function addUser(\Asiah\SiklusBundle\Entity\User $subscriber)
    {
        $this->subscriber[] = $subscriber;
    }

    /**
     * Add userRoles
     *
     * @param Asiah\SiklusBundle\Entity\Role $userRoles
     */
    public function addRole(\Asiah\SiklusBundle\Entity\Role $userRoles)
    {
        $this->userRoles[] = $userRoles;
    }
}